home *** CD-ROM | disk | FTP | other *** search
- #include <LibAUX.h>
- #include </:usr:include:signal.h>
- #include </:usr:include:errno.h>
- #include </:usr:include:fcntl.h>
- #include <Memory.h>
- #include <Stdio.h>
- #include <String.h>
- #include <Events.h>
-
- #define STACKSIZE 2048
-
-
- Handle auxfork_pipe(toparent,tochild,childtask,childargs)
- int toparent,tochild,(*childtask)(),*childargs;
- {
- Handle global_handle;
- int pid,flags,parent_pipe[2],child_pipe[2],**fake=(&toparent)+4;
- char *childstack;
- struct childinfo *globals;
-
- /* Make sure A/UX is running */
-
- if (!AUXisRunning()) return(NULL);
-
- /* allocate global area */
-
- global_handle = NewHandle(sizeof(struct childinfo));
- globals = (struct childinfo *)*global_handle;
-
- /* allocate temporary stack */
-
- globals->childstack = childstack = NewPtr(STACKSIZE);
-
- /* create pipes */
-
- if ( (toparent && (auxpipe(parent_pipe) == -1)) ||
- (tochild && (auxpipe(child_pipe) == -1)) ) {
- DisposPtr(childstack);
- DisposHandle(global_handle);
- return(NULL);
- };
-
- /* setup signals */
-
- globals->cstat = auxsignal(SIGCLD, SIG_DFL);
- globals->istat = auxsignal(SIGINT, SIG_IGN);
- globals->qstat = auxsignal(SIGQUIT, SIG_IGN);
-
- /* fork it off (I becomes we) */
-
- if((pid = auxfork(childstack+STACKSIZE,fake)) == 0) {
-
- /* this is the child process */
-
- /* create pipe from child to parent */
-
- if ( toparent ) {
- (void) auxclose(1);
- (void) auxdup(parent_pipe[1]);
- (void) auxclose(2);
- (void) auxdup(parent_pipe[1]);
- (void) auxclose(parent_pipe[0]);
- (void) auxclose(parent_pipe[1]);
- }
- else {
- (void) auxclose(1);
- (void) auxclose(2);
- };
-
- /* create pipe from parent to child */
-
- if ( tochild ) {
- (void) auxclose(0);
- (void) auxdup(child_pipe[0]);
- (void) auxclose(child_pipe[0]);
- (void) auxclose(child_pipe[1]);
- }
- else (void) auxclose(0);
-
- /* invoke child process now */
-
- (void) childtask(childargs);
- }
- else {
-
- /* this is the parent */
-
- /* check if fork failed */
-
- if (pid < 0) {
-
- if ( toparent ) {
- (void)auxclose(parent_pipe[0]);
- (void)auxclose(parent_pipe[1]);
- };
- if ( tochild ) {
- (void)auxclose(child_pipe[0]);
- (void)auxclose(child_pipe[1]);
- };
- (void) auxsignal(SIGCLD, globals->cstat);
- (void) auxsignal(SIGINT, globals->istat);
- (void) auxsignal(SIGQUIT, globals->qstat);
- DisposPtr(childstack);
- DisposHandle(global_handle);
-
- return(NULL);
- }
- else {
-
- /* fork succeeded */
-
- globals->pid = pid;
- globals->toparent = toparent ? parent_pipe[0] : 0;
- globals->tochild = tochild ? child_pipe[1] : 0;
-
- /* close unneeded pipes and set flags on toparent pipe */
-
- if ( toparent ) {
- flags = auxfcntl(parent_pipe[0],F_GETFL,0);
- (void) auxfcntl(parent_pipe[0],F_SETFL,flags | O_NDELAY);
- (void)auxclose(parent_pipe[1]);
- };
- if ( tochild ) (void)auxclose(child_pipe[0]);
-
- return(global_handle);
- }
- }
- }
-
-
- int auxcleanup_fork_pipe(global_handle)
- Handle global_handle;
- {
- int status,n,toparent,tochild;
- struct childinfo *globals;
-
- globals = (struct childinfo *)*global_handle;
-
- /* wait for child to terminate */
-
- for ( status=n=0; n!=globals->pid; n=auxwait(&status) )
- if ( n<0 && GetAUXErrno()!=EINTR ) break;
-
- /* close open pipes */
-
- if ( (toparent=globals->toparent) ) auxclose(toparent);
- if ( (tochild= globals->tochild) ) auxclose(tochild);
-
- /* restore signal handling */
-
- (void) auxsignal(SIGCLD, globals->cstat);
- (void) auxsignal(SIGINT, globals->istat);
- (void) auxsignal(SIGQUIT, globals->qstat);
-
- /* dispose of temporary stack and global handle */
-
- DisposPtr(globals->childstack);
- DisposHandle(global_handle);
-
- return(status);
- }
-
- char *auxfgets(s,n,file,timeout)
- char *s;
- int n,file,timeout;
- {
- int i;
- EventRecord theEvent;
- short mask=everyEvent;
- char *cp=s,*ep=s+n-1;
-
- for ( i=0; (i<timeout) || (timeout==0); i++ ) {
- while ( cp<ep && (auxread(file,cp,1) == 1) )
- if ( *cp == '\r' ) {
- *cp++ = '\n';
- i = timeout;
- break;
- }
- else cp++;
- *cp = '\0';
- (void) EventAvail(mask, &theEvent);
- };
- return(cp>s ? s : NULL);
- }
-